﻿using System;
using System.Windows.Forms;
using MyVocabulary.App_Code;

namespace MyVocabulary
{
    public partial class AddGrammaticalPerson : Form
    {
        #region Consts

        protected static class FormMessages
        {
            public const string ValidatePerson = "Please enter a grammatical person";
            public const string ValidateTranslation = "Please enter a translation";
        }

        public enum AddGrammaticalPersonMode
        {
            Add,
            Edit
        }

        #endregion

        #region Fields & Properties

        protected AddGrammaticalPersonMode mode;
        /// <summary>
        /// Indicate if the form is in the add or edit mode.
        /// </summary>
        public AddGrammaticalPersonMode Mode
        {
            get { return mode; }
            set
            {
                mode = value;
                Text = mode.ToString(); //Set the form title
            }
        }

        /// <summary>
        /// This object contains all the data that appears on the form.
        /// When the user presses the ok button all the values entered in the form are saved in this object.
        /// </summary>
        public Person VocabPerson { get; set; }

        /// <summary>
        /// Occurs after the user presses the 'OK' button and the data is saved into the VocabPerson property.
        /// </summary>
        public event EventHandler OnDataSaved;

        #endregion

        #region Constructors
        
        /// <summary>
        /// Create a new form
        /// </summary>
        /// <param name="Mode">Indicate if the form is in the add or edit mode</param>
        /// <param name="OnDataSaved">Occurs after the user presses the 'OK' button and the data is saved into the VocabPerson property</param>
        public AddGrammaticalPerson(AddGrammaticalPersonMode Mode, EventHandler OnDataSaved)
        {
            InitializeComponent();
            
            this.Mode = Mode;
            this.OnDataSaved = OnDataSaved;           
            VocabPerson = new Person();
        }

        /// <summary>
        /// Create a new form
        /// </summary>
        /// <param name="Mode">Indicate if the form is in the add or edit mode</param>
        /// <param name="OnDataSaved">Occurs after the user presses the 'OK' button and the data is saved into the VocabPerson property</param>
        /// <param name="VocabPerson">This object contains all the data that appears on the form.
        /// When the user presses the ok button all the values entered in the form are saved in this object.</param>
        public AddGrammaticalPerson(AddGrammaticalPersonMode Mode, EventHandler OnDataSaved, Person VocabPerson)
        {
            InitializeComponent();

            this.Mode = Mode;
            this.OnDataSaved = OnDataSaved;            
            this.VocabPerson = VocabPerson;

            tbPerson.Text = VocabPerson.Text;
            tbTranslation.Text = VocabPerson.Translation;
        }

        #endregion

        #region TextBoxes

        /// <summary>
        /// Check if the user entered a value for the grammatical person TextBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbPerson_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbPerson.Text.Trim()))
                errorProvider1.SetError(tbPerson, FormMessages.ValidatePerson);
            else
                errorProvider1.SetError(tbPerson, string.Empty);
        }

        /// <summary>
        /// Check if the user entered a value for the translation TextBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbTranslation_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbTranslation.Text.Trim()))
                errorProvider1.SetError(tbTranslation, FormMessages.ValidateTranslation);
            else
                errorProvider1.SetError(tbTranslation, string.Empty);
        }

        #endregion
        
        #region Form Shortcuts & Buttons

        /// <summary>
        /// Enable form shortcuts:
        /// * Ctrl + S - Save the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (!e.Control) return;

            switch (e.KeyCode)
            {
                case Keys.S:
                    e.Handled = true;
                    e.SuppressKeyPress = true;
                    btnOK_Click(sender, e);
                    break;
            }
        }

        /// <summary>
        /// Save the form data into the VocabPerson property and call the OnDataSaved event handler.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbPerson.Text.Trim()) || string.IsNullOrEmpty(tbTranslation.Text.Trim()))
                return;

            VocabPerson.Text = tbPerson.Text;
            VocabPerson.Translation = tbTranslation.Text;

            if (OnDataSaved != null)
                OnDataSaved(this, null);

            Close();
        }

        /// <summary>
        /// Close the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
    }
}
